<?php
    // $pic_name=date("dMYHis");  
    function ResizeImage($maxwidth,$maxheight,$new_file_name,$save_path,$file_name){
    	$temp_arr = explode(".", $file_name);
    	$file_ext = array_pop($temp_arr);
    	$file_ext = trim($file_ext);
    	$file_ext = strtolower($file_ext);
    	
    	switch ($file_ext){
    		case 'jpg':
    			$im=imagecreatefromjpeg($save_path.'/'.$file_name);
    			break;
    		case 'png':
    			$im=imagecreatefrompng($save_path.'/'.$file_name);
    			break;
    		case 'gif':
    			$im=imagecreatefromgif($save_path.'/'.$file_name);
				break;
    		default:
    			$im=imagecreatefromwbmp($save_path.'/'.$file_name);
    			break;
    	}
        //取得当前图片大小   
        $width = imagesx($im);
        $height = imagesy($im);
        
        $newwidth=$maxwidth;
        $newheight=$maxheight;
        
        $widthratio=$maxwidth/$width;
        $heightratio=$maxheight/$height;
        if ($widthratio>$heightratio) {
        	$newheight=$height*$maxwidth/$width;
        }
        else {
        	$newwidth=$width*$maxheight/$height;
        }
        
        
        //先生成缩略图，然后进行裁剪   
        if(function_exists("imagecopyresampled")){    
            $tempim = imagecreatetruecolor($newwidth, $newheight);    
            imagecopyresampled($tempim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            $newim=imagecreatetruecolor($maxwidth,$maxheight);
            imagecopy($newim, $tempim, 0, 0, 0, 0, $maxwidth, $maxheight);
        }else{    
			$tempim = imagecreate($newwidth, $newheight);    
			imagecopyresized($tempim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			$newim=imagecreatetruecolor($maxwidth,$maxheight);
			imagecopy($newim, $tempim, 0, 0, 0, 0, $maxwidth, $maxheight);
		}    
		ImageJpeg($newim,$save_path."/".$new_file_name . ".jpg");
		imagedestroy($tempim);
		ImageDestroy ($newim);
		imagedestroy($im);
    }